/******************************************************************

 SWAZAM - Homescreen Javascript

 ******************************************************************/

var rotateInterval;
var swazam;

function rotateHomeScreen() {
	var divs = ["homeContent1", "homeContent2", "homeContent3"];
	var counter = 0;

	showDiv(divs, counter);

	rotateInterval = setInterval(function () {
		counter = (counter + 1) % divs.length;
		showDiv(divs, counter);
	}, 5000);
}

function showDiv(divs, divId) {
	for (var i = 0; i < divs.length; i++) {
		$("#" + divs[i]).hide();
	}
	$("#" + divs[divId]).show();
}

function checkSession() {
	$.ajax({
		dataType:"json",
		url:"service/web/session",
		success:function (data) {
			//okay, logged in
			clearInterval(rotateInterval);
			swazam = new MySWAzam(data);
			swazam.init("#mainContent");
		}
	});
}

function performLogin() {
	$.post("service/web/login",
			{
				"username":$("#inputLoginUsername").val(),
				"password":$("#inputLoginPassword").val()
			})
			.done(function (data) {
				console.log("done");
				$("#loginModal").modal("hide");
				checkSession();
			});
}

function executeRegistration() {
	var inputUsername = $("#inputRegisterUsername").val();
	var inputPassword = $("#inputRegisterPassword").val();
	var inputFirstname = $("#inputRegisterFirstname").val();
	var inputLastname = $("#inputRegisterLastname").val();

	//TODO: perform some checks

	console.log(inputUsername);

	$.post("service/web/register",
			{
				"username":inputUsername,
				"password":inputPassword,
				"firstname":inputFirstname,
				"lastname":inputLastname
			})
			.done(function (data) {
				$("#registerModal").modal("hide");
			});
}

$(function () {
	rotateHomeScreen();
	checkSession();
	$("#loginButton").click(performLogin);
	$("#signupButton").click(executeRegistration);
});
